package com.gosophia.conferenceManagement.notification.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.DozerChangeUtil;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.exception.ConcurrencyException;
import com.gosophia.commons.exception.ParameterException;
import com.gosophia.commons.exception.TechnicalException;
import com.gosophia.conferenceManagement.ConferenceTurningAction;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.service.ConferenceHandleService;
import com.gosophia.conferenceManagement.common.ws.impl.ConferenceHandleWebServiceImpl;
import com.gosophia.conferenceManagement.notification.dto.NotificationJobDataDTO;
import com.gosophia.conferenceManagement.notification.entity.NotificationJobData;
import com.gosophia.conferenceManagement.notification.service.NotificationService;
import com.gosophia.conferenceManagement.notification.ws.NotificationWebService;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;
/**
 * 通知Webservice实现
 * @author gaoweiguang
 * @since  2011-3-26
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Service("notificationWebService")
@Transactional
public class NotificationWebServiceImpl  extends ConferenceHandleWebServiceImpl  implements NotificationWebService {

    @Autowired
    private NotificationService notificationService;
    @Autowired
    private TaskManagementService taskManagementService;
    @Autowired
    private UserManagementService userManagementService;
	@Autowired
	@Qualifier("notificationService")
	private ConferenceHandleService conferenceHandleService;
	@Override
	public void addNotificationJobData(
			NotificationJobDataDTO notificationJobDataDTO) throws BusinessException {
		 Assert.isTrue(notificationJobDataDTO != null, "notificationJobDataDTO不能为null");
	     Assert.isTrue(notificationJobDataDTO.getJobId() != null, "jobId不能为null");
	     NotificationJobData notificationJobData = new NotificationJobData();
			Job job = taskManagementService.findJobDetailByJobId(notificationJobDataDTO.getJobId());
			notificationJobData.setJob(job);
         if(notificationJobDataDTO.getNextApproval()!=null) {
        	 Long nextUserId = notificationJobDataDTO.getNextApproval().getUserInfoId();
        	 if(nextUserId!=null) {
        		 notificationJobData.setNextApproval(userManagementService.findUserInfoByUserInfoId(nextUserId));
        	 }
         }

		 notificationJobData.setNotificationType(notificationJobDataDTO.getNotificationType());
		 notificationJobData.setOpinion(notificationJobDataDTO.getOpinion());
		 notificationJobData.setTurningAction(notificationJobDataDTO.getTurningAction());
		 
		 if (notificationJobDataDTO.getOnlineDocumentDTO() != null) {
				Document document = new Document();
				document.setDocumentPath(notificationJobDataDTO.getOnlineDocumentDTO()
						.getDocumentPath());
				document.setDocumentVersion(notificationJobDataDTO.getOnlineDocumentDTO()
						.getDocumentVersion());
				document.setDocumentCategory("online");
				notificationJobData.setOnlineDocument(document);
			}
		 
		 notificationService.addNotificationJobData(notificationJobData);

	}

	@Override
	public void updateNotificationJobData(
			NotificationJobDataDTO notificationJobDataDTO) throws BusinessException {
		Assert.isTrue(notificationJobDataDTO.getJobId() != null, "jobId不能为null");
		Assert.isTrue(notificationJobDataDTO != null, "notificationJobDataDTO不能为null");
		NotificationJobData notificationJobData = (NotificationJobData) taskManagementService
		.findJobDataDetail(notificationJobDataDTO.getJobId(), NotificationJobData.class);
		UserInfo approval = null;
		if(notificationJobDataDTO.getNextApproval()!=null) {
			Long nextUserId = notificationJobDataDTO.getNextApproval().getUserInfoId();
			if(nextUserId!=null) {
				approval = userManagementService.findUserInfoByUserInfoId(nextUserId);
			}
		}
        if(notificationJobData!=null) {
        	if(approval!=null) {
        		notificationJobData.setNextApproval(approval);
        	}
        	if(notificationJobDataDTO.getOpinion()!=null) {
        		 notificationJobData.setOpinion(notificationJobDataDTO.getOpinion());
        	}
            if(notificationJobDataDTO.getTurningAction()!=null) {
            	 notificationJobData.setTurningAction(notificationJobDataDTO.getTurningAction());
            }
           
        }
        
		 if (notificationJobDataDTO.getOnlineDocumentDTO() != null) {
				Document document = new Document();
				document.setDocumentPath(notificationJobDataDTO.getOnlineDocumentDTO()
						.getDocumentPath());
				document.setDocumentVersion(notificationJobDataDTO.getOnlineDocumentDTO()
						.getDocumentVersion());
				document.setDocumentCategory("online");
				notificationJobData.setOnlineDocument(document);
			}
        conferenceHandleService.updateConferenceJobData(notificationJobData);
	}

	@Override
	public NotificationJobDataDTO findNotificationJobDataByJobId(Long jobId)
			throws BusinessException {
		Assert.notNull(jobId, "jobId不能为null");
		NotificationJobData notificationJobData = (NotificationJobData) taskManagementService
				.findJobDataDetail(jobId, NotificationJobData.class);
		if (notificationJobData == null) {
			return null;
		}
		NotificationJobDataDTO notificationJobDataDTO = DozerChangeUtil
				.changeBeforeToAfter(notificationJobData, NotificationJobDataDTO.class);
		if(notificationJobData.getOnlineDocument() != null){
			DocumentDTO docuemntDTO = DozerChangeUtil.changeBeforeToAfter(
					notificationJobData.getOnlineDocument(), DocumentDTO.class);
			notificationJobDataDTO.setOnlineDocumentDTO(docuemntDTO);
		}
		
		JobDTO jobDTO = DozerChangeUtil.changeBeforeToAfter(notificationJobData
				.getJob(), JobDTO.class);
		if(notificationJobData.getJob().getOperator() != null){
			UserInfoDTO user = DozerChangeUtil.changeBeforeToAfter(notificationJobData
					.getJob().getOperator(), UserInfoDTO.class);
			jobDTO.setOperatorDTO(user);
		}
		
		notificationJobDataDTO.setJobDTO(jobDTO);
		notificationJobDataDTO.setJobId(jobId);
		return notificationJobDataDTO;
	}

	@Override
	public NotificationJobDataDTO findPreNotificationJobDataByJobId(Long jobId)
			throws BusinessException {
		
		Assert.notNull(jobId, "jobId不能为null");
		Job job = taskManagementService.findJobDetailByJobId(jobId);
		Job preJob = job.getPreviousJob();
		if (preJob == null) {
			return null;
		}
		NotificationJobData notificationJobData = (NotificationJobData) taskManagementService
				.findJobDataDetail(preJob.getJobId(), NotificationJobData.class);
		if (notificationJobData == null) {
			return null;
		}
		NotificationJobDataDTO notificationJobDataDTO = DozerChangeUtil
		.changeBeforeToAfter(notificationJobData, NotificationJobDataDTO.class);
        if(notificationJobData.getOnlineDocument() != null){
	        DocumentDTO docuemntDTO = DozerChangeUtil.changeBeforeToAfter(
			notificationJobData.getOnlineDocument(), DocumentDTO.class);
	        notificationJobDataDTO.setOnlineDocumentDTO(docuemntDTO);
         }

         JobDTO jobDTO = DozerChangeUtil.changeBeforeToAfter(notificationJobData
		.getJob(), JobDTO.class);
         notificationJobDataDTO.setJobDTO(jobDTO);
        if(notificationJobData.getJob().getOperator() != null){
	         UserInfoDTO user = DozerChangeUtil.changeBeforeToAfter(notificationJobData
			.getJob().getOperator(), UserInfoDTO.class);
	      jobDTO.setOperatorDTO(user);
        }

        notificationJobDataDTO.setJobId(jobId);
		return notificationJobDataDTO;
         }


	@Override
	public List<JobDTO> findFinishedNotificationApprovalHistoryRecordByJobId(
			Long jobId,String noticeType) throws BusinessException, ParameterException,
			ConcurrencyException, TechnicalException {
		Assert.notNull(jobId, "jobId不能为null");
		Job job = taskManagementService.findJobDetailByJobId(jobId);
		Long missionId = job.getMission().getMissionId();
		List<Job> finishedHistoryList = new ArrayList<Job>();
		if("0".equals(noticeType)) {
			 finishedHistoryList = taskManagementService
			   .findJobByMissionTypeAndJobTypeFinished(missionId, "预通知审批");	
		} else if("1".equals(noticeType)) {
			 finishedHistoryList = taskManagementService
				.findJobByMissionTypeAndJobTypeFinished(missionId, "正式通知审批");	
		}
		List<JobDTO> historyDTOs = new ArrayList<JobDTO>();
		if (finishedHistoryList != null && finishedHistoryList.size() != 0) {
			for (Job j : finishedHistoryList) {
				JobDTO jDTO = DozerChangeUtil.changeBeforeToAfter(j,
						JobDTO.class);
				UserInfoDTO approval = DozerChangeUtil.changeBeforeToAfter(j
						.getOperator(), UserInfoDTO.class);
				jDTO.setOperatorDTO(approval);
				historyDTOs.add(jDTO);
			}
		}

		return historyDTOs;
	}

	@Override
	public void HandleConferenceTurning(Long jobId, Long operatorId,
			NotificationJobDataDTO notificationJobDataDTO)
			throws BusinessException {
         Assert.notNull(jobId, "jobId不能为null");
         Assert.notNull(notificationJobDataDTO, "data不能为null");
         Assert.notNull(notificationJobDataDTO.getTurningAction(), "流转action不能为null");
         Assert.notNull(operatorId,"进行流转时操作人Id不能为空");
         NotificationJobData notificationJobData = new NotificationJobData();
	    
		 if(ConferenceTurningAction.INFORMAL_NOTIFICATION.getValue().equals(notificationJobDataDTO.getTurningAction())
			|| ConferenceTurningAction.NOTIFICATION.getValue().equals(notificationJobDataDTO.getTurningAction())) {
			 NotificationJobData notificationJobData1 = (NotificationJobData) taskManagementService
				.findJobDataDetail(jobId, NotificationJobData.class);
                if(notificationJobData1!=null) {
                	notificationJobData = notificationJobData1;
                }
		 }
		 Job job = taskManagementService.findJobDetailByJobId(jobId);
	     notificationJobData.setJob(job);
		 Document doc = new Document();
		 doc.setDocumentPath(notificationJobDataDTO.getOnlineDocumentDTO().getDocumentPath());
		 doc.setDocumentVersion(notificationJobDataDTO.getOnlineDocumentDTO().getDocumentVersion());
		 notificationJobData.setOnlineDocument(doc);
		 if(notificationJobDataDTO.getNextApproval()!=null) {
			 Long nextUserId = notificationJobDataDTO.getNextApproval().getUserInfoId();
			 notificationJobData.setNextApproval(userManagementService.findUserInfoByUserInfoId(nextUserId));
		 }
		
		 notificationJobData.setNotificationType(notificationJobDataDTO.getNotificationType());
		 notificationJobData.setOpinion(notificationJobDataDTO.getOpinion());
		 notificationJobData.setTurningAction(notificationJobDataDTO.getTurningAction());
		 notificationJobData.setNotificationType(notificationJobDataDTO.getNotificationType());
		 conferenceHandleService.HandleConferenceTurning(jobId, operatorId, notificationJobData);
	}


}
